/**
	2011/12/03 satmat
	twitter id : satellitesat
*/

/**
	2ch番組表へ接続、データ取得を行う
*/
function EPG2ch(){
};

EPG2ch.prototype = new XMLHttpRequest();
EPG2ch.prototype.EPG_2CH_TOP_URL = 'http://epg.2ch.net/tv2chwiki/pukiwiki.php';
EPG2ch.prototype.EPG_2CH_BASE_URI = 'http://epg.2ch.net/tv2chwiki/';

	/**
		id=content_1_0のh2要素以降から、現在番組情報を取得して
		key-valueを返す。
		curh2 はDOMツリー中の <h2 id="content_1_0"> への参照とする。
	*/
EPG2ch.prototype.parseCurrentProgram = function(curh2){
	var anc = new Array();
	var data = new Object();
	var p = null;

	p = curh2.nextSibling;

	/* Aタグの取得 */
	while(curh2.nextSibling){
		if(p.tagName && p.tagName.toLowerCase()=="a"){
			anc.push( p );
		}else if(p.tagName && p.tagName.toLowerCase()=="h2" &&
					p.id && p.id=="content_1_1"){
			break; // 現在番組表終了付近まで繰り返し、終了する
		}
		p = p.nextSibling;
	}

	data[this.epgTable.NHK] 				=  anc[0].innerText;
	data[this.epgTable.NHK_TIM] 		=  anc[1].previousSibling.nodeValue;
	data[this.epgTable.NHK_TIT] 		=  anc[1].innerText;
	data[this.epgTable.ETV] 				=  anc[2].innerText;
	data[this.epgTable.ETV_TIM] 		=  anc[3].previousSibling.nodeValue;
	data[this.epgTable.ETV_TIT] 		=  anc[3].innerText;
	data[this.epgTable.NTV] 				=  anc[4].innerText;
	data[this.epgTable.NTV_TIM] 		=  anc[5].previousSibling.nodeValue;
	data[this.epgTable.NTV_TIT] 		=  anc[5].innerText;
	data[this.epgTable.TBS] 				=  anc[6].innerText;
	data[this.epgTable.TBS_TIM] 		=  anc[7].previousSibling.nodeValue;
	data[this.epgTable.TBS_TIT] 		=  anc[7].innerText;
	data[this.epgTable.FUJI] 				=  anc[8].innerText;
	data[this.epgTable.FUJI_TIM] 		=  anc[9].previousSibling.nodeValue;
	data[this.epgTable.FUJI_TIT] 		=  anc[9].innerText;
	data[this.epgTable.ASA] 				=  anc[10].innerText;
	data[this.epgTable.ASA_TIM] 		=  anc[11].previousSibling.nodeValue;
	data[this.epgTable.ASA_TIT] 		=  anc[11].innerText;
	data[this.epgTable.TOKYO] 			=  anc[12].innerText;
	data[this.epgTable.TOKYO_TIM] 	=  anc[13].previousSibling.nodeValue;
	data[this.epgTable.TOKYO_TIT] 	=  anc[13].innerText;
	
	return data;
};
	
	/**
	2ch番組表取得用XMLHttpRequestのreadyStateChangeハンドラ
	*/
EPG2ch.prototype.onreadystatechange = function(event){
	switch(event.target.readyState){
		case 0:
			break;	
		case 1:
			if(this.epgTable && this.epgTable.showBusy()){
				this.epgTable.showBusy();
			}
			break;
		case 2:
			if(this.epgTable && this.epgTable.showBusy()){
				this.epgTable.showBusy();
			}
			break;
		case 3:
			if(this.epgTable && this.epgTable.showBusy()){
				this.epgTable.showBusy();
			}
			break;
		case 4:
			if(this.status == 200){
//				console.log(this.responseText);
				console.log('HTTP 200 OK');
			
				// 本当はDOMParserとか使うべきだが、厳密なxmlでないとパースできないので
				// とりあえずdivのinnerHTMLに流しこんでみる。
				var resdiv = document.createElement("div");
				resdiv.innerHTML = event.target.responseText;
				var h2elems = resdiv.getElementsByTagName("h2");
				var curh2 = null;					
				for(var i=0; i<h2elems.length; i++){
					if(h2elems[i].id == "content_1_0"){
						curh2 = h2elems[i];
						break;
					}
				}
				
				if(this.epgTable && this.epgTable.addPrograms){
					var data = this.parseCurrentProgram(curh2);
					this.epgTable.addPrograms(data);
				}else{
					console.error('this.epgTable malformed.');
				}				
			}else{
				console.error('XMLHttpRequest failed. status code ' + this.status);
			}
			break;
	}
};

/**
	2ch番組表へ接続する関数
 */
EPG2ch.prototype.connect = function(){		
	this.open('GET', this.EPG_2CH_TOP_URL, true);
	this.send();
};


/**
	現在番組表
*/
function CurrentEPGTable(tid){
	this.date = new Date();
	this.epgtable = document.getElementById(tid);
	if(!this.epgtable){
		throw 'id:'+tid+'is malformed.';
	}
}

CurrentEPGTable.prototype =  {	
	NHK : 'nhk_st',
	ETV : 'evt_st',
	NTV : 'ntv_st',
	TBS : 'tbs_st',
	FUJI : 'fuji_st',
	ASA : 'asa_st',
	TOKYO : 'tokyo_st',
	
	NHK_TIM : 'nhk_tim',
	ETV_TIM : 'evt_tim',
	NTV_TIM : 'ntv_tim',
	TBS_TIM : 'tbs_tim',
	FUJI_TIM : 'fuji_tim',
	ASA_TIM : 'asa_tim',
	TOKYO_TIM : 'tokyo_tim',

	NHK_TIT : 'nhk_tit',
	ETV_TIT : 'evt_tit',
	NTV_TIT : 'ntv_tit',
	TBS_TIT : 'tbs_tit',
	FUJI_TIT : 'fuji_tit',
	ASA_TIT : 'asa_tit',
	TOKYO_TIT : 'tokyo_tit',

	addPrograms : function(epgdata){
		this.epgtable.innerHTML = '<span><a href="http://jk.nicovideo.jp/watch/jk1" target="_blank">' 
										+ epgdata[this.NHK] + '</a></span>'
										+ '<span>' + epgdata[this.NHK_TIM] + '</span>'
										+ '<span>' + epgdata[this.NHK_TIT] + '</span><br>';										
		this.epgtable.innerHTML += '<span><a href="http://jk.nicovideo.jp/watch/jk2" target="_blank">' 
										+ epgdata[this.ETV] + '</a></span>'
										+ '<span>' + epgdata[this.ETV_TIM] + '</span>'
										+ '<span>' + epgdata[this.ETV_TIT] + '</span><br>';
		this.epgtable.innerHTML += '<span><a href="http://jk.nicovideo.jp/watch/jk4" target="_blank">'
										 + epgdata[this.NTV] + '</a></span>'
										+ '<span>' + epgdata[this.NTV_TIM] + '</span>'
										+ '<span>' + epgdata[this.NTV_TIT] + '</span><br>';
		this.epgtable.innerHTML += '<span><a href="http://jk.nicovideo.jp/watch/jk5" target="_blank">'
										 + epgdata[this.ASA] + '</a></span>'
										+ '<span>' + epgdata[this.ASA_TIM] + '</span>'
										+ '<span>' + epgdata[this.ASA_TIT] + '</span><br>';
		this.epgtable.innerHTML += '<span><a href="http://jk.nicovideo.jp/watch/jk6" target="_blank">'
										 + epgdata[this.TBS] + '</a></span>'
										+ '<span>' + epgdata[this.TBS_TIM] + '</span>'
										+ '<span>' + epgdata[this.TBS_TIT] + '</span><br>';
		this.epgtable.innerHTML += '<span><a href="http://jk.nicovideo.jp/watch/jk7" target="_blank">'
										 + epgdata[this.TOKYO] + '</a></span>'
										+ '<span>' + epgdata[this.TOKYO_TIM] + '</span>'
										+ '<span>' + epgdata[this.TOKYO_TIT] + '</span><br>';
		this.epgtable.innerHTML += '<span><a href="http://jk.nicovideo.jp/watch/jk8" target="_blank">'
										 + epgdata[this.FUJI] + '</a></span>'
										+ '<span>' + epgdata[this.FUJI_TIM] + '</span>'
										+ '<span>' + epgdata[this.FUJI_TIT] + '</span><br>';	
	},
	
	showBusy : function(){
		if(this.epgtable.innerText==''){
			this.epgtable.innerText = 'データ取得中.';
		}else{
			this.epgtable.innerText += '.';
		}
	},
	
	showFailed : function(){
		this.epgtable.innerText = '番組表の取得に失敗しましたorz';
	}
}


/**
	popup画面のとりまとめクラス
*/
function Popup(){
	this.curepg = new CurrentEPGTable('currentepg');
	this.epg2ch = new EPG2ch();
}

Popup.prototype = {
	initialize: function(){
		EPG2ch.prototype.epgTable = this.curepg;
		this.epg2ch.connect();
	}
}

/**
	popupページ読み込み完了後の動作
*/
function load(event){
	console.log('popup.html loaded.');
	setTimeout( function(){
		console.log('settimeout fired.');
		var popup = new Popup();
		popup.initialize();
	}, 1000);
}


/**
	拡張アイコンクリック時の動作
*/
window.addEventListener('load', load, true);
